Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bucket.Put: copy key before seek #637

Merged
merged 3 commits into from
Dec 13, 2023
Merged

Conversation

fuweid
Copy link
Member

@fuweid fuweid commented Dec 12, 2023

Application might change key value after seeking and before real put.
This unexpected behaviour could corrupt database. When users file issue,
maintainers doesn't know application behaviour. It could be caused by
data race. This patch is to prevent such case and save maintainers' time.

REF: #72

@ahrtr ahrtr added this to the v1.4.0 milestone Dec 12, 2023
tests/failpoint/db_failpoint_test.go Outdated Show resolved Hide resolved
tests/robustness/issue72_test.go Outdated Show resolved Hide resolved
tests/robustness/issue72_test.go Outdated Show resolved Hide resolved
Signed-off-by: Wei Fu <fuweid89@gmail.com>
Comment on lines 161 to 163
// When application inserts key/value, that key can be changed by application
// during insertion. bbolt should copy key before seeking and updating. Otherwise,
// some branch node could point to freed page.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't necessarily cause elements to point freed pages. In this test case, if you change the key to a larger key e.g. 200, the case will pass.

Suggested change
// When application inserts key/value, that key can be changed by application
// during insertion. bbolt should copy key before seeking and updating. Otherwise,
// some branch node could point to freed page.
// When bbolt is processing a `Put` invocation, the key might be concurrently
// updated by the application which calls the `Put` API (although it shouldn't).
// It might lead to a situation that bbolt use an old key to find a proper
// position to insert the key/value pair, but actually inserts a new key.
// Eventually it might break the rule that all keys should be sorted. In a
// worse case, it might cause page elements to point to already freed pages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment. Updated

Signed-off-by: Wei Fu <fuweid89@gmail.com>
Application might change key value after seeking and before real put.
This unexpected behaviour could corrupt database. When users file issue,
maintainers doesn't know application behaviour. It could be caused by
data race. This patch is to prevent such case and save maintainers' time.

Signed-off-by: Wei Fu <fuweid89@gmail.com>
Copy link
Member

@ahrtr ahrtr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Thanks @fuweid

Please backport the PR to release-1.3 as well.

@ahrtr ahrtr merged commit cd4a757 into etcd-io:main Dec 13, 2023
15 checks passed
fuweid added a commit to fuweid/bbolt that referenced this pull request Dec 17, 2023
It's follow-up of etcd-io#637.

Signed-off-by: Wei Fu <fuweid89@gmail.com>
fuweid added a commit to fuweid/bbolt that referenced this pull request Dec 17, 2023
It's follow-up of etcd-io#637.

Signed-off-by: Wei Fu <fuweid89@gmail.com>
fuweid added a commit to fuweid/bbolt that referenced this pull request Dec 18, 2023
It's follow-up of etcd-io#637.

Signed-off-by: Wei Fu <fuweid89@gmail.com>
(cherry picked from commit 62d8026)
Signed-off-by: Wei Fu <fuweid89@gmail.com>
@fuweid fuweid deleted the copy-key-before-seek branch December 18, 2023 15:42
fuweid added a commit to fuweid/bbolt that referenced this pull request Dec 18, 2023
- [bucket: allow to allocate key on stack in Put()](etcd-io#550)
- [bucket.Put: copy key before seek](etcd-io#637)
- [copy key before comparing during CreateBucket or CreateBucketIfNotExists](etcd-io#641)

Signed-off-by: Wei Fu <fuweid89@gmail.com>
fuweid added a commit to fuweid/bbolt that referenced this pull request Dec 18, 2023
- [bucket: allow to allocate key on stack in Put()](etcd-io#550)
- [bucket.Put: copy key before seek](etcd-io#637)
- [copy key before comparing during CreateBucket or CreateBucketIfNotExists](etcd-io#641)

Signed-off-by: Wei Fu <fuweid89@gmail.com>
@ahrtr ahrtr removed this from the v1.4.0 milestone Jan 12, 2024
ishan16696 pushed a commit to ishan16696/bbolt that referenced this pull request Feb 25, 2024
It's follow-up of etcd-io#637.

Signed-off-by: Wei Fu <fuweid89@gmail.com>
ishan16696 pushed a commit to ishan16696/bbolt that referenced this pull request Feb 25, 2024
It's follow-up of etcd-io#637.

Signed-off-by: Wei Fu <fuweid89@gmail.com>
ishan16696 pushed a commit to ishan16696/bbolt that referenced this pull request Feb 25, 2024
- [bucket: allow to allocate key on stack in Put()](etcd-io#550)
- [bucket.Put: copy key before seek](etcd-io#637)
- [copy key before comparing during CreateBucket or CreateBucketIfNotExists](etcd-io#641)

Signed-off-by: Wei Fu <fuweid89@gmail.com>
passiondev2024 added a commit to passiondev2024/bbolt that referenced this pull request Mar 20, 2024
- [bucket: allow to allocate key on stack in Put()](etcd-io/bbolt#550)
- [bucket.Put: copy key before seek](etcd-io/bbolt#637)
- [copy key before comparing during CreateBucket or CreateBucketIfNotExists](etcd-io/bbolt#641)

Signed-off-by: Wei Fu <fuweid89@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants